﻿#pragma execution_character_set("utf-8")
#include "login.h"
#include "ui_login.h"
#include <QPainter>
#include <QDebug>
#include "dialog.h"

login::login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);

    // 设置窗口标题和图标
    setWindowTitle("登录");
    setWindowIcon(QIcon("logo.ico"));
    
    // 设置背景透明，确保背景图片能显示
    setStyleSheet("background: transparent;");
    
    // 设置控件背景透明
    ui->label->setStyleSheet("background: transparent;");
    ui->label_conn->setStyleSheet("background: transparent;");
    ui->lineEdit_account->setStyleSheet("background: rgba(255, 255, 255, 180); border-radius: 5px;");
    ui->lineEdit_passage->setStyleSheet("background: rgba(255, 255, 255, 180); border-radius: 5px;");
    ui->pushButton_login->setStyleSheet("background-color: rgba(0, 120, 215, 200); color: white; border-radius: 5px;");
    ui->pushButton_register->setStyleSheet("background-color: rgba(0, 120, 215, 200); color: white; border-radius: 5px;");

    socket = new QTcpSocket(this);
    connect(ui->pushButton_register,SIGNAL(clicked()),this,SLOT(signalSLots()));
    connect(ui->pushButton_login,SIGNAL(clicked()),this,SLOT(signalSLots()));

    socket->connectToHost("127.0.0.1",9966);


    //连接提示
    connect(socket,SIGNAL(connected()),this,SLOT(connectedSlot()));
    //断提示
    connect(socket,SIGNAL(disconnected()),this,SLOT(disconnectedSlot()));
    //收到"0 1"进行登录
    connect(socket,SIGNAL(readyRead()),this,SLOT(readSLOT()));


}

login::~login()
{
    delete ui;
}

void login::signalSLots()
{
    if(sender()==ui->pushButton_register)
    {
        if(ui->label->text()=="登录界面")
        {
            QMessageBox::information(this,"提示","开始注册");
            ui->pushButton_login->setEnabled(false);
            ui->label->setText("注册界面");
        }
        else if(ui->label->text()=="注册界面")
        {
            if(ui->lineEdit_account->text()==""||ui->lineEdit_passage->text()=="")
            {
                QMessageBox::information(this,"提示","注册失败请输入账号或密码");
                return ;
            }
            QMessageBox::information(this,"提示","注册成功");
            ui->pushButton_login->setEnabled(true);
            ui->label->setText("登录界面");
            //账号密码存入数据库
            QString text;
            text.append(ui->lineEdit_account->text()).append("/").append(ui->lineEdit_passage->text()).append("/").append("0");
            QByteArray buffer = text.toUtf8();
            // 发送
            socket->write(buffer);


        }



    }
    else if(sender()==ui->pushButton_login)
    {
        ui->label->setText("登录界面");
        //账号密码存入数据库
        QString text;
        text.append(ui->lineEdit_account->text()).append("/").append(ui->lineEdit_passage->text()).append("/").append("1");
        QByteArray buffer = text.toUtf8();
        // 发送
        socket->write(buffer);


    }
    else
    {}

}

void login::connectedSlot()
{

    ui->label_conn->setText("连接成功");

}

void login::disconnectedSlot()
{
    ui->label_conn->setText("断开连接");
}

void login::readSLOT()
{
    QByteArray buffer = socket->readAll();
    // QByteArray → QString
    QString text(buffer);

    if(text=="1")
    {
        Dialog *w=new Dialog;
        w->show();
        close();


    }else if(text=="0")
    {

        QMessageBox::warning(this,"警告","账号或密码输入错误，请重新输入");
    }

}

// 添加绘制背景的事件处理函数
void login::paintEvent(QPaintEvent *event)
{
    // 加载背景图片 - 使用绝对路径
    QPixmap pixmap("E:\\MyCode\\Qt_Projects\\client\\backgroundpic.jpg");
    
    // 检查图片是否成功加载
    if (pixmap.isNull()) {
        qDebug() << "Failed to load background image!";
        QDialog::paintEvent(event);
        return;
    }
    
    // 缩放图片以适应窗口大小
    QPixmap scaledPixmap = pixmap.scaled(this->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    // 创建画家对象
    QPainter painter(this);
    // 绘制背景图片
    painter.drawPixmap(0, 0, scaledPixmap);
    // 绘制其他部件
    QDialog::paintEvent(event);
}
